package com.zs.studentmanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zs.studentmanager.common.ResponseCodeEnum;
import com.zs.studentmanager.entity.domain.StudentScore;
import com.zs.studentmanager.entity.vo.EchartsSeriesVo;
import com.zs.studentmanager.entity.vo.PageVo;
import com.zs.studentmanager.entity.vo.ScoreVo;
import com.zs.studentmanager.exception.CustomException;
import com.zs.studentmanager.mapper.StudentScoreMapper;
import com.zs.studentmanager.service.StudentScoreService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
* @author 周小白
* @description 针对表【s_student_score(学生成绩表)】的数据库操作Service实现
* @createDate 2024-01-19 19:58:37
*/
@Service
public class StudentScoreServiceImpl extends ServiceImpl<StudentScoreMapper, StudentScore>
    implements StudentScoreService{

    private final StudentScoreMapper studentScoreMapper;

    public StudentScoreServiceImpl(StudentScoreMapper studentScoreMapper) {
        this.studentScoreMapper = studentScoreMapper;
    }




    /**
     * 获取成绩列表
     * @param pageNum 页码
     * @param pageSize 每页数量
     * @param searchValue 搜索值
     * @return  成绩列表
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageVo<ScoreVo> getScoreList(
            Integer pageNum, Integer pageSize, String searchValue, Integer courseId, Integer gradeClassId
    ) {
        if (pageNum == null || pageSize == null) {
            throw new CustomException(ResponseCodeEnum.NO_PARAM);
        }
        if (pageNum < 1 || pageSize < 1) {
            throw new CustomException("参数错误");
        }

        // 查询
        List<ScoreVo> list = studentScoreMapper.getScoreList(
                pageNum, pageSize, searchValue, courseId, gradeClassId
        );
        if (list == null) {
            throw new CustomException(ResponseCodeEnum.SYSTEM_ERROR);
        }

        // 查询总数
        Long count = studentScoreMapper.selectCount(null);


        // 封装分页对象
        PageVo<ScoreVo> pageVo = new PageVo<>();
        pageVo.setList(list);
        pageVo.setTotal(count);

        return pageVo;
    }



    /**
     * 获取统计数据
     * @param courseId 课程id
     * @param gradeClassId 班级id
     * @return 统计数据
     */
    @Override
    public List<EchartsSeriesVo> getCensusData(Integer courseId, Integer gradeClassId) {
        List<EchartsSeriesVo> list = new ArrayList<>();

        // 根据课程id和班级id查询成绩列表
        LambdaQueryWrapper<StudentScore> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StudentScore::getCourseId, courseId);
        queryWrapper.eq(StudentScore::getGradeclassId, gradeClassId);
        List<StudentScore> scoreList = studentScoreMapper.selectList(queryWrapper);
        if (scoreList == null){
            throw new CustomException(ResponseCodeEnum.SYSTEM_ERROR);
        }

        // 成绩为优秀的数量
        AtomicInteger excellentCount = new AtomicInteger();
        EchartsSeriesVo excellentEcharts = new EchartsSeriesVo();

        // 成绩为良好的数量
        AtomicInteger goodCount = new AtomicInteger();
        EchartsSeriesVo goodEcharts = new EchartsSeriesVo();

        // 成绩为一般的数量
        AtomicInteger generalCount = new AtomicInteger();
        EchartsSeriesVo generalEcharts = new EchartsSeriesVo();

        // 成绩为较差的数量
        AtomicInteger poorCount = new AtomicInteger();
        EchartsSeriesVo poorEcharts = new EchartsSeriesVo();

        // 成绩为不及格的数量
        AtomicInteger failCount = new AtomicInteger();
        EchartsSeriesVo failEcharts = new EchartsSeriesVo();

        // 遍历成绩列表
        scoreList.forEach(item -> {
            if (item.getScore() >= 90){
                excellentCount.getAndIncrement();
                excellentEcharts.setName("优秀");
                excellentEcharts.setValue(excellentCount.intValue());
            }else if (item.getScore() >= 80){
                goodCount.getAndIncrement();
                goodEcharts.setName("良好");
                goodEcharts.setValue(goodCount.intValue());
            }else if (item.getScore() >= 70){
                generalCount.getAndIncrement();
                generalEcharts.setName("一般");
                generalEcharts.setValue(generalCount.intValue());
            }else if (item.getScore() >= 60){
                poorCount.getAndIncrement();
                poorEcharts.setName("较差");
                poorEcharts.setValue(poorCount.intValue());
            }else {
                failCount.getAndIncrement();
                failEcharts.setName("不及格");
                failEcharts.setValue(failCount.intValue());
            }
        });

        // 添加到集合
        if (excellentCount.intValue() != 0){
            list.add(excellentEcharts);
        }
        if (goodCount.intValue() != 0){
            list.add(goodEcharts);
        }
        if (generalCount.intValue() != 0){
            list.add(generalEcharts);
        }
        if (poorCount.intValue() != 0){
            list.add(poorEcharts);
        }
        if (failCount.intValue() != 0){
            list.add(failEcharts);
        }



        return list;
    }


}




